package com.ufaud.ais.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ufaud.ais.pub.dao.IGenericCRUDDao;
import com.ufaud.ais.pub.exception.BusinessException;
import com.ufaud.ais.pub.service.impl.GenericCRUDServiceImpl;
import com.ufaud.ais.sys.entity.CustomItem;
import com.ufaud.ais.sys.entity.CustomItemType;
import com.ufaud.ais.sys.service.ICustomItemService;

public class CustomItemServiceImpl extends GenericCRUDServiceImpl<CustomItem, Long> implements ICustomItemService {

	private IGenericCRUDDao<CustomItemType, Long> customItemTypeDao;

	public CustomItemServiceImpl(IGenericCRUDDao<CustomItem, Long> genericDao) {
		super(genericDao);
	}

	private IGenericCRUDDao<CustomItem, Long> getItemDao() {
		return iGenericCRUDDao;
	}

	private IGenericCRUDDao<CustomItemType, Long> getItemTypeDao() {
		return customItemTypeDao;
	}

	public CustomItemType getTypeById(Long id) {
		return getItemTypeDao().get(id);
	}

	public CustomItemType getTypeByName(String name) {
		return null;
	}

	public List<CustomItemType> getAllType() {
		return getItemTypeDao().getAll();
	}

	public void saveItemType(CustomItemType customItemType) {
		getItemTypeDao().save(customItemType);
	}

	public void deleteItemTypeById(Long id) throws BusinessException {
		CustomItemType persistedItemType = getItemTypeDao().get(id);
		Set<CustomItem> customItemSet = persistedItemType.getCustomItemSet();
		if (customItemSet != null && customItemSet.size() > 0) {
			throw new BusinessException(persistedItemType.getItemTypeName() + "尚有自定义项数据，无法删除");
		}
		getItemTypeDao().remove(id);
	}

	public List<CustomItem> getItemListByTypeId(Long typeId) {
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("typeId", typeId);
		List<CustomItem> list = getItemDao().findByNamedQuery("getItemListByTypeId", queryParams);
		return list;
	}

	public List<CustomItem> getItemListByTypeName(String typeName) {
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("typeName", typeName);
		List<CustomItem> list = getItemDao().findByNamedQuery("getItemListByTypeName", queryParams);
		return list;
	}

	public void insertType(CustomItemType customItemType) {
		getItemTypeDao().save(customItemType);
	}

	public void deleteTypeById(Long typeId) throws BusinessException {
		CustomItemType persistedItemType = getItemTypeDao().get(typeId);
		if (persistedItemType.getCustomItemSet().size() > 0) {
			throw new BusinessException("该类型下尚有未删除的项，不允许删除");
		}
		getItemTypeDao().remove(typeId);
	}

	public void updateType(CustomItemType customItemType) {
		Long id = customItemType.getItemTypeId();
		CustomItemType persistedItemType = getItemTypeDao().get(id);
		persistedItemType.setItemTypeName(customItemType.getItemTypeName());
		persistedItemType.setItemTypeDesc(customItemType.getItemTypeDesc());
	}

	public void deleteItemById(Long itemId) throws BusinessException {
		// TODO
		getItemDao().remove(itemId);
	}

	public void updateItem(CustomItem customItem) {
		Long id = customItem.getItemId();
		CustomItem persistedItem = getItemDao().get(id);
		persistedItem.setItemName(customItem.getItemName());
		persistedItem.setItemDesc(customItem.getItemDesc());
	}

	public void setCustomItemTypeDao(IGenericCRUDDao<CustomItemType, Long> customItemTypeDao) {
		this.customItemTypeDao = customItemTypeDao;
	}
}
